﻿using Dapper.DBContext;
using Guoc.BigMall.WebService.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Guoc.BigMall.Domain.Entity;

namespace Guoc.BigMall.WebService.Implement
{
    public class MemberAddressService : IMemberAddressService
    {
        IUserContextService _userContext;
        IDBContext _db;
        public MemberAddressService(IDBContext dbContext, IUserContextService userContext)
        {
            this._db = dbContext;
            _userContext = userContext;

        }

        public int CreateOrUpdate(MemberAddressDto model)
        {
            // 判断是否设置默认地址，只能有一个默认地址
            if (model.IsDefault)
            {
                var allAddress = _db.Table<MemberAddress>().Where(n => n.MemberId == model.MemberId && n.IsDefault == true).ToList();
                if (allAddress.Count > 0)
                {
                    allAddress.ForEach(item => item.IsDefault = false);
                    _db.Update(allAddress.ToArray());
                }
            }

            var entity = _db.Table<MemberAddress>().FirstOrDefault(n => n.Id == model.Id);
            if (entity == null)
            {
                entity = Mapper.Map<MemberAddress>(model);
                _db.Insert(entity);
            }
            else
            {
                entity = Mapper.Map<MemberAddress>(model);
                _db.Update(entity);
            }


            _db.SaveChange();
            return entity.Id;
        }

        public void Delete(int addressId)
        {
            _db.Delete<MemberAddress>(n => n.Id == addressId);
            _db.SaveChange();
        }

        public List<MemberAddressListVo> GetAddressList(int memberId)
        {
            var sql = @"select ma.Id,ma.IsDefault,ma.Name,ma.Tel, CONCAT(ma.AreaFullName,ma.Address)  as Address 
from MemberAddress ma  where ma.MemberId =@MemberId order by ma.IsDefault desc";

            var rows = _db.DataBase.Query<MemberAddressListVo>(sql, new { MemberId = memberId }).ToList();
            return rows;
        }

        public MemberAddressVo GetById(int addressId)
        {
            var sql = @"select ma.Id,ma.MemberId,ma.AreaId as AreaCode,ma.IsDefault,ma.PostCode,ma.Name,ma.Tel,ma.Address as AddressDetail ,ma.AreaFullName
from MemberAddress ma  where ma.Id =@AddressId";

            var model = _db.DataBase.QuerySingle<MemberAddressVo>(sql, new { AddressId = addressId });
            if (model == null)
            {
                model = new MemberAddressVo();
            }
            return model;
        }

        public MemberAddressVo GetDefaultAddress(int memberId)
        {
            var sql = @"select ma.Id,ma.MemberId,ma.AreaId as AreaCode,ma.IsDefault,ma.PostCode,ma.Name,ma.Tel,ma.Address as AddressDetail ,ma.AreaFullName
from MemberAddress ma  where ma.MemberId =@MemberId and ma.IsDefault = 1";

            var model = _db.DataBase.QuerySingle<MemberAddressVo>(sql, new { MemberId = memberId });
            if (model == null)
            {
                model = new MemberAddressVo();
            }
            return model;
        }
    }
}
